#include <iostream>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,m;
ll p;
int w[N];
struct node
{
    int l,r;
    ll sum,add,mul;
}tr[N*4];

void pushup(int u)
{
    tr[u].sum = (tr[u<<1].sum + tr[u<<1|1].sum) % p;
}

void build(int u,int l,int r)
{
    if(l==r)
    {
        tr[u] = {l,r,w[l],0,1};
    }
    else
    {
        tr[u] = {l,r,0,0,1};
        int mid = l + r >> 1;
        build(u<<1, l, mid);
        build(u<<1|1, mid+1, r);
        pushup(u);
    }
}

void eval(node &t, int add, int mul)
{
    t.sum=(t.sum * mul + add * (ll)(t.r - t.l + 1)) % p;
    t.mul = t.mul * mul % p;
    t.add = (t.add * mul + add) % p;
}

void pushdown(int u)
{
    eval(tr[u<<1], tr[u].add, tr[u].mul);
    eval(tr[u<<1|1], tr[u].add, tr[u].mul);
    tr[u].add=0; tr[u].mul=1;
}

void modify(int u,int l,int r,int add,int mul)
{
    if(tr[u].l >= l && tr[u].r <= r)
    {
        eval(tr[u], add, mul);
    }
    else
    {
        pushdown(u);
        int mid=tr[u].l + tr[u].r >> 1;
        if(l<=mid) modify(u<<1, l, r, add, mul);
        if(r>mid) modify(u<<1|1, l, r, add, mul);
        pushup(u);
    }
}

ll query(int u, int l, int r)
{
    if(tr[u].l>=l && tr[u].r <=r)
    {
        return tr[u].sum;
    }
    else
    {
        pushdown(u);
        int mid=tr[u].l + tr[u].r >>1;
        ll res=0;
        if(l<=mid) res+=query(u<<1 , l ,r);
        if(r>mid) res=(res + query(u<<1|1, l, r)) % p;
        return res;
    }
}
int main()
{
    scanf("%d %lld",&n,&p);
    for(int i=1;i<=n;i++) scanf("%d",w+i);
    build(1,1,n);
    scanf("%d",&m);
    int op,t,g;
    int c;
    while(m--)
    {
        scanf("%d %d %d",&op,&t,&g);
        if(op==1)
        {
            scanf("%d",&c);
            modify(1,t,g,0,c);
        }
        else if(op==2)
        {
            scanf("%d",&c);
            modify(1,t,g,c,1);
        }
        else
        {
            printf("%lld\n", query(1, t, g));
        }
    }

    return 0;
}
